/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import model.Etudiant;
import model.Cour;

/**
 *
 * @author Paul
 */
public class DAOEtudiant extends DAO<Etudiant> {

    public void create(Etudiant obj) {
        String query2 = "INSERT INTO `personne`( `nom`, `prenom`, `dateNaissance`, `typeUtilisateur`, `login`, `password`) VALUES (?,?,?,?,?,?);";
        int nb = 0;
        int autoIncKeyFromApi = -1;
        PreparedStatement ps = null;
        /**
         * lancement de la connexion à la bd et de la requete *
         */
        try {
            Connection conn = DBConnection.getConnection();//Creation de la connexion
            ps = conn.prepareStatement(query2, Statement.RETURN_GENERATED_KEYS);//preparation de la requete

            /**
             * remplacement des points d'interogation dans la query par les
             * variables*
             */
            ps.setString(1, obj.getNom());
            ps.setString(2, obj.getPrenom());
            ps.setDate(3, obj.getDatenaissance());
            ps.setString(4, obj.getTypeUtilisateur());
            ps.setString(5, obj.getLogin());
            ps.setString(6, obj.getMdp());
            nb = ps.executeUpdate();//lancement de la requête
            ResultSet rs = ps.getGeneratedKeys();//recupération du résultat
            if (rs.next()) {//récuperation de l'id 
                autoIncKeyFromApi = rs.getInt(1);
                System.out.println(autoIncKeyFromApi);
                autoIncKeyFromApi = rs.getInt(1);
                System.out.println(autoIncKeyFromApi);
                String query = "INSERT INTO `etudiant`(`id_personne`) VALUES (" + autoIncKeyFromApi + ");";
                System.out.println(query);
                ps = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);//preparation de la requete
                nb = ps.executeUpdate();
                System.out.println(nb);
            }

        } catch (SQLException e) {
            e.printStackTrace();

        }


    }

    public void update(Etudiant obj) {
        String query = "";
        changeGeneral(query);
    }

    public void delete(Etudiant obj) {
        String query = "";
        changeGeneral(query);
    }

    public ArrayList<Etudiant> getAll() {
        String query = "";
        return findGeneral(query);
    }

    public Etudiant find(int id) {
        String query = "";
        Etudiant etudiant = null;
        etudiant = findGeneral(query).get(0);
        return etudiant;
    }

//---------------------------------------------------------private method--------------------------------------------------------------------//
    private int changeGeneral(String query) {
        Statement st = null;
        int nb = 0;
        try {
            Connection conn = DBConnection.getConnection();
            st = conn.createStatement();
            nb = st.executeUpdate(query);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return nb;
    }

    /**
     * Permet la suppression d'une entrée de la base
     *
     * @param obj
     */
    private ArrayList<Etudiant> findGeneral(String requete) {
        ArrayList<Etudiant> etudiants = new ArrayList<Etudiant>();
        Statement st = null;
        Etudiant etudiant = null;
        return etudiants;
    }

    public ArrayList<Cour> findCours(int idEleve) throws SQLException {
        PreparedStatement st = null;
        ArrayList<Cour> listeCours = new ArrayList<Cour>();
        try {
            String query = "select c.nom from cour c, etudiant e where e.id_eleve='1'";
            //String query = "select c.nom from cour c, etudiant e where e.id_eleve='" + idEleve + "'";
            Connection conn = DBConnection.getConnection();
            st = conn.prepareStatement(query);
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                String nomCours = rs.getString("nom");
                Cour c = new Cour(nomCours);
                listeCours.add(c);
            }
            System.out.println("L'étudiant a les cours suivants :/n");
            for (Cour cours : listeCours) {
                System.out.println(cours.getNomCour());
            }
        } finally {
            if (st != null) {
                st.close();
            }
        }
        return listeCours;
    }
}
